﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRB_0._1
{
    class Animation
    {
        Texture2D spriteStrip;

        float scale;

        int elapsedTime, frameTime, frameCount, currentFrame;

        Color color;

        Rectangle sourceRect = new Rectangle();

        Rectangle destinationRect = new Rectangle();

        public int FrameWidth, FrameHeight, WidthOffset, HeightOffset, WidthSpacing;

        public bool Active, Looping, left2right;

        public Vector2 Position;

        public void Initialize(Texture2D texture, Vector2 position,
            int frameWidth, int frameHeight, int widthOffset, int heightOffset, int widthSpacing, int frameCount,
            int frametime, Color color, float scale, bool looping, bool left2right)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.WidthOffset = widthOffset;
            this.HeightOffset = heightOffset;
            this.WidthSpacing = widthSpacing;
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.scale = scale;
            this.left2right = left2right;

            Looping = looping;
            Position = position;
            spriteStrip = texture;

            elapsedTime = 0;
            if (left2right)
                currentFrame = 0;
            else
                currentFrame = frameCount - 1;

            Active = true;
        }
        public void Update(GameTime gameTime)
        {
            if (Active == false)
                return;

            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (elapsedTime > frameTime)
            {
                if (left2right)
                {
                    currentFrame++;
                    if (currentFrame == frameCount)
                    {
                        currentFrame = 0;

                        if (Looping == false)
                            Active = false;
                    }
                    elapsedTime = 0;
                }
                else
                {
                    currentFrame--;
                    if (currentFrame == 0)
                    {
                        currentFrame = frameCount - 1;

                        if (Looping == false)
                            Active = false;
                    }
                    elapsedTime = 0;
                }
            }
            sourceRect = new Rectangle(currentFrame * (WidthSpacing + FrameWidth) + WidthOffset, 0 + HeightOffset, FrameWidth, FrameHeight);

            destinationRect = new Rectangle((int)Position.X,
                (int)Position.Y,
                (int)(FrameWidth * scale),
                (int)(FrameHeight * scale));
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
        }
    }
}
